﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Managers;
using System.Collections;
using Model;
using System.Windows;
using System.Windows.Input;
using MyAdressBookWPF.Helpers;
using System.ComponentModel;
using Microsoft.Win32;

namespace MyAdressBookWPF.ViewModel
{
    public class ContactViewModel : ViewModelBase
    {
        #region Private Fields
        private UserDataManager _userDataManager;
        private IList<Contacts> _contactList;
        private Contacts _selectedContact;
        private Contacts _infoContact;
        private bool _saveCommandIsEnabled;

        private ICommand _saveCommand;
        private ICommand _deleteCommand;
        private ICommand _cleanseCommand;

        private string _contactName;
        private string _contactFirstName;
        private string _contactAddress;
        private string _contactTel;

        
        
        #endregion

        #region Contructors
        public ContactViewModel(UserDataManager userDataManager)
        {
            _userDataManager = userDataManager;
            ContactsList = GetContacts();
            
        }

        
        
        #endregion

        #region Properties
        public IList<Contacts> ContactsList
        {
            get 
            { 
                return _contactList;
            }
            set 
            {
                if (_contactList != value)
                {
                    _contactList = value;
                    OnPropertyChanged("ContactsList");
                }
            }
        }

        public Contacts SelectedContact
        {
            get {return _selectedContact;}
            set
            {
                _selectedContact = value;
                OnPropertyChanged("SelectedContact");
                if (_selectedContact == null)
                {
                    if (ContactName == "" || ContactFirstName == "")
                    {
                        SaveCommandIsEnabled = false;
                    }
                }
                else
                {
                    InfoContact = SelectedContact;
                    ContactName = SelectedContact.ContactName;
                    ContactFirstName = SelectedContact.ContactFirstName;
                    ContactAddress = SelectedContact.ContactAdress;
                    ContactTel = SelectedContact.ContactTel;
                    SaveCommandIsEnabled = true;
                }
            }
        }

        public string ContactName
        {
            get
            {
                return _contactName;
            }
            set
            {
                _contactName = value;
                OnPropertyChanged("ContactName");
                if (ContactName != "" && ContactFirstName != "")
                {
                    SaveCommandIsEnabled = true;
                }
                else if (ContactFirstName == "")
                    SaveCommandIsEnabled = false;
            }
        }
        public string ContactFirstName
        {
            get
            {
                return _contactFirstName;
            }
            set
            {
                _contactFirstName = value;
                OnPropertyChanged("ContactFirstName");
                if (ContactName != "" && ContactFirstName != "")
                {
                    SaveCommandIsEnabled = true;
                }
                else if (ContactFirstName == "")
                    SaveCommandIsEnabled = false;

            }
        }
        public string ContactAddress
        {
            get
            {
                return _contactAddress;
            }
            set
            {
                _contactAddress = value;
                OnPropertyChanged("ContactAddress");
            }
        }
        public string ContactTel
        {
            get
            {
                return _contactTel;
            }
            set
            {
                _contactTel = value;
                OnPropertyChanged("ContactTel");
            }
        }

        public Contacts InfoContact
        {
            get
            {
                return _infoContact;
            }
            set
            {       
                _infoContact = value;
                OnPropertyChanged("InfoContact");
            }
        }

        

        public bool SaveCommandIsEnabled
        {
            get { return _saveCommandIsEnabled; }
            set
            {
                _saveCommandIsEnabled = value;
                OnPropertyChanged("SaveCommandIsEnabled");
            }
        }
        #endregion

        #region Command

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    SaveCommand = new Command(ExecuteSaveCommand);
                }
                return _saveCommand;
            }
            set
            {
                _saveCommand = value;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    DeleteCommand = new Command(ExecuteDeleteCommand);
                }
                return _deleteCommand;
            }
            set
            {
                _deleteCommand = value;
            }
        }
        
        public ICommand CleanseCommand
        {
            get
            {
                if (_cleanseCommand == null)
                {
                    CleanseCommand = new Command(ExecuteCleanseCommand);
                }
                return _cleanseCommand;
            }
            set
            {
                _cleanseCommand = value;
            }
        }

        #endregion

        #region Privates Methods
        public IList<Contacts> GetContacts()
        {
            var result = _userDataManager.ContactList;
            return result;
        }

        public void ExecuteSaveCommand()
        {
            
                if (SelectedContact != null)
                {
                    if (SelectedContact.ContactName == "" || SelectedContact.ContactFirstName == "")
                        MessageBox.Show("ContactName and ContactFirstName's fields can't be empties");
                    else
                    {
                        SelectedContact.ContactName = ContactName;
                        SelectedContact.ContactFirstName = ContactFirstName;
                        SelectedContact.ContactAdress = ContactAddress;
                        SelectedContact.ContactTel = ContactTel;
                        _userDataManager.ModifyContact(SelectedContact);
                    }
                }
                else
                {
                    InfoContact = new Contacts
                    {
                        ContactName = ContactName,
                        ContactFirstName = ContactFirstName,
                        ContactAdress = ContactAddress,
                        ContactTel = ContactTel
                    };

                    if (InfoContact.ContactFirstName == "" || InfoContact.ContactName == "")
                        MessageBox.Show("ContactName and ContactFirstName's fields can't be empties");
                    else
                        _userDataManager.AddContact(InfoContact);
                }
                ContactsList = _userDataManager.GetAllContact();
        }

        public void ExecuteDeleteCommand() 
        {
            _userDataManager.DeleteContact(SelectedContact);
            ContactsList = GetContacts();
        }

        public void ExecuteCleanseCommand()
        {
            SelectedContact = null;
            InfoContact = null;
            ContactTel = "";
            ContactName = "";
            ContactFirstName = "";
            ContactAddress = "";
            SaveCommandIsEnabled = false;
            OpenFileDialog dialog = new OpenFileDialog();
        }
        #endregion
    }
}
